﻿<?php
// query för att hitta markers inom 25 km 
/*$query = "SELECT id, ( 6371 * acos( cos( radians($lat) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians($lng) ) + sin( radians($lat) ) * sin( radians( lat ) ) ) ) AS distance FROM markers HAVING distance < $km ORDER BY distance LIMIT 0 , 20";*/

require_once("initialize.php");

if(isset($_GET['action'])){
	$action = $_GET['action'];
	switch ($action) {
    case "get_toppstopps":
				$latlng = $_GET['latlng'];
				$radius = $_GET['radius'];
        $category = $_GET['category'];
				get_toppstopps($latlng,$radius,$category);
        break;
	}
}

function get_toppstopps($latlng,$radius,$category) {
	$latlng;
	$radius;
  $category;
	$toppstoppstotal = array();
	foreach ($latlng as $point){
	$lat = $point[0];
	$lng = $point[1];
	$sql = "SELECT id, name, lat, lng, ( 6371 * acos( cos( radians( ".$lat." ) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians( ".$lng." ) ) + sin( radians( ".$lat." ) ) * sin( radians( lat ) ) ) ) AS distance FROM toppstopps HAVING distance < ".$radius." ORDER BY distance LIMIT 0 , 20";

	$toppstopps = toppstopp::find_by_sql($sql);
	$toppstoppstotal = implode("," , array_unique(array_merge(explode("'",$toppstoppstotal), explode("'",$toppstopps))));

  echo $toppstoppstotal;
	}	
	/*header('Content-type: application/json');
	header('Accept-charset: utf-8');
	echo json_encode($lat);*/

}

function get_categories(){
	global $database;
	$sql = "SELECT * FROM categories";
	$result_set = $database->query($sql);
	while ($row = $database->fetch_array($result_set)) {
		echo "<li id='category-" . $row['id'] . "' class='sprite-" . $row['name'] . " inactive' title='" . $row['title']."' ></li>";
	}	
}

function strip_zeros_from_date( $marked_string="" ) {
  // first remove the marked zeros
  $no_zeros = str_replace('*0', '', $marked_string);
  // then remove any remaining marks
  $cleaned_string = str_replace('*', '', $no_zeros);
  return $cleaned_string;
}

function redirect_to( $location = NULL ) {
  if ($location != NULL) {
    header("Location: {$location}");
    exit;
  }
}

function output_message($message="") {
  if (!empty($message)) { 
    return "<p class=\"message\">{$message}</p>";
  } else {
    return "";
  }
}

function __autoload($class_name) {
	$class_name = strtolower($class_name);
  $path = LIB_PATH.DS."{$class_name}.php";
  if(file_exists($path)) {
    require_once($path);
  } else {
		die("The file {$class_name}.php could not be found.");
	}
}

function include_layout_template($template="") {
	include(LAYOUTS_PATH.DS.$template);
}

function log_action($action, $message="") {
	$logfile = SITE_ROOT.DS.'logs'.DS.'log.txt';
	$new = file_exists($logfile) ? false : true;
  if($handle = fopen($logfile, 'a')) { // append
    $timestamp = strftime("%Y-%m-%d %H:%M:%S", time());
		$content = "{$timestamp} | {$action}: {$message}\n";
    fwrite($handle, $content);
    fclose($handle);
    if($new) { chmod($logfile, 0755); }
  } else {
    echo "Could not open log file for writing.";
  }
}

function datetime_to_text($datetime="") {
  $unixdatetime = strtotime($datetime);
  return strftime("%B %d, %Y at %I:%M %p", $unixdatetime);
}



?>